package cn.iocoder.an.module.business.service.producttablemapping;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.an.module.business.controller.admin.producttablemapping.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.producttablemapping.ProductTableMappingDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.producttablemapping.ProductTableMappingMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 产品表名映射 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ProductTableMappingServiceImpl implements ProductTableMappingService {

    @Resource
    private ProductTableMappingMapper productTableMappingMapper;

    @Override
    public String createProductTableMapping(ProductTableMappingSaveReqVO createReqVO) {
        // 插入
        ProductTableMappingDO productTableMapping = BeanUtils.toBean(createReqVO, ProductTableMappingDO.class);
        productTableMappingMapper.insert(productTableMapping);

        // 返回
        return productTableMapping.getProductId();
    }

    @Override
    public void updateProductTableMapping(ProductTableMappingSaveReqVO updateReqVO) {
        // 校验存在
        validateProductTableMappingExists(updateReqVO.getProductId());
        // 更新
        ProductTableMappingDO updateObj = BeanUtils.toBean(updateReqVO, ProductTableMappingDO.class);
        productTableMappingMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductTableMapping(String id) {
        // 校验存在
        validateProductTableMappingExists(id);
        // 删除
        productTableMappingMapper.deleteById(id);
    }

    @Override
        public void deleteProductTableMappingListByIds(List<String> ids) {
        // 删除
        productTableMappingMapper.deleteByIds(ids);
        }


    private void validateProductTableMappingExists(String id) {
        if (productTableMappingMapper.selectById(id) == null) {
            throw exception(PRODUCT_TABLE_MAPPING_NOT_EXISTS);
        }
    }

    @Override
    public ProductTableMappingDO getProductTableMapping(String id) {
        return productTableMappingMapper.selectById(id);
    }

    @Override
    public PageResult<ProductTableMappingDO> getProductTableMappingPage(ProductTableMappingPageReqVO pageReqVO) {
        return productTableMappingMapper.selectPage(pageReqVO);
    }

}